/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxbusinessrelcrapi.p                               */
/* PROCEDURE TYPE        : supplier API Process                             */
/* DESCRIPTION           : businessrelation Create API Process              */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                               04-Jul-20   */
/* CREATED BY            : Gaoyan                               21-OCT-24   */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/
{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{us/gp/gpuid.i}


{xxjson.i}
{xxcimload.i}

{xxapilog.i}


Define input  parameter m_traceid            as character no-undo.
Define input  parameter m_import             as logical   no-undo.
Define output parameter m_status             as logical   no-undo.
Define output parameter m_errmsg             as character no-undo.

define variable v_apiresult           as character                no-undo.
define variable v_id                  as integer  init 0 no-undo.
define variable v_errflag             as logical   initial yes    no-undo.
define variable v_curtaskid           as character                no-undo.
define variable v_cnt                 as integer                  no-undo.
define variable m_error               as integer                  no-undo.
define variable m_wrng                as integer                  no-undo.



/* ======== for api ========= */
define variable vireturn                     as integer     no-undo.
define variable ilreturndataset              as logical     no-undo.
define variable ilpartialupdate              as logical     no-undo.
define variable icpartialupdateexceptionlist as character   no-undo.
define variable ocprimarykey                 as character   no-undo.
define variable ocrowid                      as character   no-undo.
define variable oidraftinstance              as integer     no-undo.
define variable ocprimarykeyname             as character   no-undo.
define variable vho                          as handle.
define variable oireturnstatus               as integer     no-undo. 
define variable m_company                    like company.companycode no-undo.
define variable m_busnrelid1                 as integer no-undo.
define variable m_addid                      as integer no-undo.
define variable m_length                     as integer no-undo.


{ proxy/bbusinessrelationdef.i}

define shared temp-table tt_tbusinessrelation like tbusinessrelation
              field tt_traceid as character
              field tt_dataid as character.
define shared temp-table tt_taddress          like taddress
              field tt_businessrelationcode as character
              field tt_traceid as character
              field tt_dataid as character.
define shared temp-table tt_tcontact          like tcontact
                  field tt_businessrelationcode as character
                  field tt_addressname as character
                  field tt_traceid as character
                  field tt_dataid as character.
define shared temp-table tt_tfcmessages       like tfcmessages.


EMPTY TEMP-TABLE  tBusinessRelation.
EMPTY TEMP-TABLE  tAddress.
EMPTY TEMP-TABLE  tContact.

   output to /home/mfg/xxbrcr.log.
   
   put unformatted "m_traceid: " m_traceid skip.
   
   for each tt_tbusinessrelation no-lock /* where tt_tbusinessrelation.tt_traceid = m_traceid */:  
   export tt_tbusinessrelation.
   
	for first BusinessRelation where BusinessRelation.BusinessRelationCode = tt_tbusinessrelation.BusinessRelationCode no-lock:
	end.
	put unformatted available BusinessRelation  skip.
	/*** BusinessRelation not exist then create businessrelation ***/
	if not available BusinessRelation 
	then do:
	
	
    create tbusinessrelation.
    buffer-copy tt_tbusinessrelation except tt_traceid tt_dataid to tbusinessrelation.
    assign 
          tbusinessrelation.lastmodifieddate           = today
          tbusinessrelation.lastmodifiedtime           = time
          tbusinessrelation.lastmodifieduser           = global_userid
          tbusinessrelation.businessrelation_id        = next-value(objectnumber).


   
   for each tt_taddress where tt_taddress.tc_parentrowid = tt_tbusinessrelation.tc_rowid.   
	create taddress .
   buffer-copy tt_taddress except tt_traceid tt_dataid tt_businessrelationcode to tAddress.
   assign taddress.address_id = next-value(objectnumber).
         
   	
   
   for each tt_tcontact where tt_tcontact.tc_parentrowid  = tt_taddress.tc_rowid
                          .   
        create tcontact.
        buffer-copy tt_tcontact except tt_traceid tt_dataid tt_businessrelationcode tt_addressname to tcontact.
        assign tcontact.contact_id = next-value(objectnumber).
            
     end. /* If not available businessrelation */
	
   end. /* for each tt_taddress where tt_taddress.tt_businessrelationcode = tt_tbusinessrelation.BusinessRelationCode.   */
    
    put unformatted "Before run br api  "  m_import   skip.
    if m_import 
    then do:		
    /* call api to create business relation */
    run proxy/bbusinessrelation.p persistent set vhproxycomponent.
    put "aproxy/bbusinessrelatio" skip.
    run apimaintainbydatasetwithoutput in vhproxycomponent
            ('', /* company code if blank then takes default */
             'save',
             true,
             true,
             '',
             dataset bbusinessrelation by-reference,
             output ocprimarykey,
             output ocrowid,
             output oidraftinstance,
             output ocprimarykeyname,
             output dataset-handle vho,
             output dataset tfcmessages,
             output vireturn).
             
	put "after run api" skip.
   
	delete procedure vhproxycomponent.
      for each tfcmessages no-lock:
      export tfcmessages.
		create tt_tfcmessages.
		buffer-copy tfcmessages to tt_tfcmessages.   
	  end.
    /* determine error status */
    m_errmsg = "".	
    put vireturn skip.
    if vireturn = 0 then 
	assign m_error = 0
	       m_status = no.	
    if vireturn <> 0 
    then do:
        assign
            m_error = 0.
      
        for each tfcmessages no-lock:

            if tfcmessages.tcfctype = "e" then
            do:
                 m_error = -1.
				 m_status = yes.
     			 v_cnt = v_cnt + 1.
    			   if tfcmessages.tcfcfieldname <> "" then
                      v_apiresult = v_apiresult  + "|" + 
    					    gettermlabel("severity",8) + " = " +
                      string(tfcmessages.tifcseverity) + " | " +
                      tfcmessages.tcfcmessage + " | " +
                      gettermlabel("field_name",10) + " = " +
                      tfcmessages.tcfcfieldname + " | " +
                      gettermlabel("field_label",11) + " = " +
                      tfcmessages.tcfcfieldlabel + " | " +
                      gettermlabel("field_value",11) + " = " +
                      tfcmessages.tcfcfieldvalue + " | " +
                      tfcmessages.tcfcmsgnumber.
                       else
                       v_apiresult  = v_apiresult  + "|" + 
        					                  gettermlabel("severity",8) + " = " +
                                              string(tfcmessages.tifcseverity) + " | " +
                                              tfcmessages.tcfcmessage + " | " +
                                              tfcmessages.tcfcmsgnumber.
                        
                    /*  message v_apiresult view-as alert-box. */
    				
                     if v_cnt > 200 then leave.            
    	 
            end. /* if tfcmessages.tcfctype = "e" then */
    
            if tfcmessages.tcfctype = "w" then 
            do:
                m_wrng = -1.
            end. /*if warning*/
        end. /* for each tfcmessages no-lock */
         m_errmsg = v_apiresult.
         
          
         
    end. /* vireturn <> 0 */	
	
end. /* if m_import then do */

end.  /* if not available BusinessRelation */
   if available BusinessRelation then 
   do:
     m_errmsg = "".
     m_status = no.
   end.
end. /* for each tt_tbusinessrelation no-lock where tt_tbusinessrelation.tt_traceid = m_traceid:  */